package com.da.lon.wang.xlg.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.HomeActivity;
import com.da.lon.wang.xlg.bean.SkuListResult.SkuInfo;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.view.ProductsPopupWindow.CallBack3;

public class SureSkuPopupWindow extends PopupWindow implements OnClickListener {

    private View mMenuView;
    private Context mContext;
    private String phone;
    private CallBack3 myCallBack;
    private TextView update_sku_tv;
    private TextView sure_sku_tv;
    private String sku_name;
    private TextView sku_name_tv;
    private SkuInfo datas;
    // 0表示选择的 第一个 1表示选择的列表
    private int type;
    private int position;
    private int arg2;

    public SureSkuPopupWindow(Context context, SkuInfo datas, int type,
                              int position, int arg2) {
        super(context);
        this.mContext = context;
        this.datas = datas;
        this.type = type;
        this.arg2 = arg2;
        this.position = position;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(R.layout.sure_sku_popupwindow, null);

        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int mMenuView_height = mMenuView.findViewById(R.id.pop_layout)
                        .getHeight();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height || y > (height + mMenuView_height)) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        sku_name_tv = (TextView) mMenuView.findViewById(R.id.sku_name_tv);
        if (type == 0 && null != datas && null != datas.getRecommend_info()
                && null != datas.getRecommend_info().getStore_name()) {
            sku_name_tv.setText(datas.getRecommend_info().getStore_name());
        } else if (type == 1 && null != datas && null != datas.getArea_list()) {
            sku_name_tv.setText(datas.getArea_list().get(position - 1)
                    .getStore_list().get(arg2).getStore_name());
        }
        update_sku_tv = (TextView) mMenuView.findViewById(R.id.update_sku_tv);
        sure_sku_tv = (TextView) mMenuView.findViewById(R.id.sure_sku_tv);
        update_sku_tv.setOnClickListener(this);
        sure_sku_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 更改
            case R.id.update_sku_tv:
                dismiss();
                break;
            // 确认
            case R.id.sure_sku_tv:
                if (type == 0) {
//				String state = datas.getRecommend_info().getState();
//				if (!state.isEmpty() && "1".equals(state)) {
                    PreferenceUtils.getInstance().SetSettingString(
                            PreferencesConfig.STORE_ID,
                            datas.getRecommend_info().getStore_id());
                    PreferenceUtils.getInstance().SetSettingString(
                            PreferencesConfig.STORE_NAME,
                            datas.getRecommend_info().getStore_name());
                    Intent i = new Intent(mContext, HomeActivity.class);
                    mContext.startActivity(i);
                    ((Activity) mContext).finish();
//				} else {
//					ToastUtils.showShort(mContext, "亲,"
//							+ datas.getRecommend_info().getStore_name()
//							+ "馆正在装修，不能选择！");
//				}
                } else if (type == 1) {
//				String state = datas.getArea_list().get(position - 1)
//						.getStore_list().get(arg2).getState();
//				if (!state.isEmpty() && "1".equals(state)) {
                    PreferenceUtils.getInstance().SetSettingString(
                            PreferencesConfig.STORE_ID,
                            datas.getArea_list().get(position - 1)
                                    .getStore_list().get(arg2).getStore_id());
                    PreferenceUtils.getInstance().SetSettingString(
                            PreferencesConfig.STORE_NAME,
                            datas.getArea_list().get(position - 1)
                                    .getStore_list().get(arg2).getStore_name());
                    Intent i = new Intent(mContext, HomeActivity.class);
                    mContext.startActivity(i);
                    ((Activity) mContext).finish();
//				} else {
//					ToastUtils.showShort(mContext, "亲,"
//							+ datas.getArea_list().get(position - 1)
//									.getStore_list().get(arg2).getStore_name()
//							+ "馆正在装修，不能选择！");
//				}
                }
                break;

            default:
                break;
        }
    }

}
